package com.rency.ues.webapp.common;

import org.apache.commons.lang.StringUtils;
import org.rency.common.utils.domain.RespBody;
import org.rency.ues.common.UesDict;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class BaseAction implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8745714826482655611L;
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name="webProperty")
	private WebProperty webProperty;
	
	protected final RespBody respBody = new RespBody();

    protected final Map<String, Object> respData = new HashMap<String, Object>();
    
    public BaseAction(){
        respBody.setMessage(UesDict.CHINESE_SUCCESS_MSG);
    	respBody.setSuccess(true);
    	respBody.putAll(respData);
        this.clear();
    }
    
    protected ModelAndView view(String prefix,String url){
    	return new ModelAndView(prefix+url,Constants.RESPONSE_KEY,respBody);
    }
    
    /**
     * 跳转
     * @param url
     * @return
     */
    protected ModelAndView redirect(String url){
    	url = StringUtils.isBlank(url) ? Constants.URL_HOME : url;
    	ModelAndView mv = new ModelAndView("redirect:"+url,Constants.RESPONSE_KEY,respBody);
    	return mv;
    }

    /**
     * 返回成功
     * @param returnMessage
     */
    protected void success(String returnMessage){
    	respBody.setSuccess(true);
        respBody.setMessage(returnMessage);
    }
    
    /**
     * 返回失败
    * @Title: failed 
    * @Description: TODO
    * @Date: 2015年6月4日 下午9:58:01
    * @param errorMessage
     */
    protected void failed(String errorMessage){
    	respBody.setSuccess(false);
    	respBody.setMessage(errorMessage);
    }

    /**
     * 清空返回报文体
     */
    protected void clear(){
        respBody.setMessage(null);
        respBody.setCallback("");
        respBody.getData().clear();
    }
}
